<%@page import="com.tubeonfire.util.ApplicationHelper"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.google.appengine.api.users.User"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="com.tubeonfire.entity.Advertising"%>
<%@page import="com.tubeonfire.model.AdvertisingModel"%>
<%@page import="com.tubeonfire.entity.Channel"%>
<%@page import="com.tubeonfire.model.ChannelModel"%>
<%@page import="com.tubeonfire.model.admin.SiteConfigModel"%>
<%@page import="com.tubeonfire.entity.SiteConfig"%>
<%@page import="com.tubeonfire.search.TubeSearchModel"%>
<%@page import="com.tubeonfire.entity.Playlist"%>
<%@page import="com.tubeonfire.model.PlaylistModel"%>
<%@page import="com.tubeonfire.entity.Tube"%>
<%@page import="com.tubeonfire.model.SpecialTubeModel"%>
<%@page import="com.tubeonfire.entity.SpecialTube"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Locale"%>
<%@page import="java.util.List" %>
<%	
	request.setCharacterEncoding("UTF-8");
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser(); 	
	SiteConfig siteConfig = SiteConfigModel.get();	
	String logoUrl = siteConfig.getLogoKey();
	if(!logoUrl.equals("/images/logo.png")){ 
		logoUrl = "http://" + request.getServerName() + "/image?key=" + logoUrl + "&size=250";	
	}
	String siteName = siteConfig.getSiteName();
	String pageUrl = (String)request.getAttribute("url");
	String pageTitle = siteConfig.getSiteTitle();
	String pageName = siteConfig.getSiteName();
	String pageDescription = siteConfig.getSiteDescription();
	String pageKeyword = siteConfig.getSiteKeyword();
	String pageImage = logoUrl;
	String facebookAdminId = siteConfig.getFacebookAdminId();	
	String pageLogoUrl = logoUrl;
	String pageLogoSlogan = siteConfig.getLogoSlogan();
	String analyticCode = siteConfig.getAnalyticCode();
	List<String> listColour = new ArrayList<String>();
	listColour.add("green");	
	listColour.add("yellow");	
	listColour.add("blue");
	listColour.add("pink");
	listColour.add("orange");
	int place = 1;
%>
<jsp:include page="/element/header.jsp">	
	<jsp:param name="siteName" value="<%= siteName %>"/>	
	<jsp:param name="pageUrl" value="<%= pageUrl %>"/>
	<jsp:param name="pageTitle" value="<%=pageTitle %>"/>	
	<jsp:param name="pageName" value="<%=pageName %>"/>
	<jsp:param name="pageImages" value="<%=pageImage %>"/>
	<jsp:param name="pageKeyword" value="<%=pageKeyword %>"/>
	<jsp:param name="pageDescription" value="<%=pageDescription %>"/>
	<jsp:param name="facebookAdminId" value="<%=facebookAdminId %>"/>
	<jsp:param name="pageLogoUrl" value="<%=pageLogoUrl %>"/>
	<jsp:param name="pageLogoSlogan" value="<%=pageLogoSlogan %>"/>
</jsp:include>
	<!-- Screen CSS -->
	<link rel="stylesheet" href="/css/aviaslider.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="/css/prettyPhoto.css" type="text/css" media="screen" />
    <!-- AVIASLIDER - PRETTYPHOTO -->	
<header>        	
	<div id="header">
		<div id="header_inner">
			<ul class='aviaslider' id="<%=siteConfig.getSlideStyle()%>">       			 	
		    <%	
				TubeSearchModel slideModel = new TubeSearchModel();
				slideModel.prepareBanner();
				if (slideModel.getListResult().size() >0) {
					for (Tube tub : slideModel.getListResult()) {								
						if (tub != null) {
							String imgUrl = tub.getImageUrl();
							if(!imgUrl.contains("http://")){
								imgUrl = "/image?key=" + imgUrl + "&size=640";													
							}else{
								imgUrl = "http://i.ytimg.com/vi/"+ tub.getId() +"/sddefault.jpg";
							}
						%>
							<li>
								<a href="/video/<%=tub.getId() %>/<%=tub.getAlias() %>.html" title="<%=tub.getTitle()%>">
			                		<img src="<%=imgUrl %>" alt="<%=tub.getTitle()%>" title="<%=tub.getTitle()%>"/>
			              		</a>
			              	</li>                
						<%									
						}
					}
				}					
			%>		                                            			               	
			</ul>
		</div>
	</div>
</header>
<div id="content">
	<div id="content_inner">
    	<div id="leftsidecontent">            	
        <%		
        	boolean newApp = true;
        	if(siteConfig.getHomePageDisplayObject()==2){
				ChannelModel cModel = new ChannelModel();			
				cModel.setLimit(siteConfig.getHomePageBlockQuantity());
				cModel.prepareList();	
				if(cModel.getListResult().size()>0){
					int colour = 0;		
					int position = 1;
					for(Channel channel : cModel.getListResult()){
						TubeSearchModel model = new TubeSearchModel(); 
						model.setLimit(siteConfig.getHomePageTubePerBlockQuantity());
                        model.setPage(1);
                        model.prepareHomePageTubeByChannel(channel.getId());
                        if(model.getListResult().size()>0){
                        	newApp = false;
                        	Advertising obj = AdvertisingModel.getByPlaceAndPosition(place, position);
		%>
	    	<section>
	        	<div class="block">
	            	<h3 class="button <%=listColour.get(colour)%>"><a href="/channel/<%=channel.getId() %>"><%=channel.getTitle() %></a><a class="viewall" href="/channel/<%=channel.getId()%>">View All</a></h3>
					<div class="video-listing clearafter">
                	<%	                            	
						for(Tube tub : model.getListResult()){	                            		
							String imgUrl = tub.getImageUrl();
							if(!imgUrl.contains("http://")){
								imgUrl = "/image?key=" + imgUrl + "&size=195";															
							}
					%>
						<div class="video-items" id="<%=tub.getId()%>">	                                													
			                <div class="video-items_inner">
								<a href="/video/<%=tub.getId() %>/<%=tub.getAlias()%>.html" title="<%=tub.getTitle()%>" class="image-resize">
									<img src="<%=imgUrl %>" alt="<%=tub.getTitle()%>" title="<%=tub.getTitle()%>"/>
								</a>               
								<h3 class="video-title"><a href="/video/<%=tub.getId() %>/<%=tub.getAlias() %>.html"><%=tub.getSubTitle() %></a></h3>
								<div class="preview">
									<a href="/video/<%=tub.getId() %>/<%=tub.getAlias() %>.html" class="button <%=listColour.get(colour)%> time"><span class="icon-clock"><%=tub.getTotalTime()%></span></a>
	                            	<a href="http://www.youtube.com/embed/<%=tub.getId() %>?autoplay=1&loop=1&playlist=<%=tub.getId() %>" class="button <%=listColour.get(colour)%> repeat various fancybox.iframe"><span class="icon-monitor">Cinematic</span></a>
	                            </div>
							</div>
		                </div>  
                	<%
						}
					%>                                
                	</div>
				</div>
			</section>
			<%
				if(obj!=null&&obj.getId().length()>0){
			%>
			<div class="advWrap adv-468x60 clearafter" style="border: none;">
				<div>
				<%
					if(obj.getType()==1){
				%>
					<%=obj.getContent() %>	
				<%
					}else{
						String imgUrl = obj.getContent();
						imgUrl = "/image?key=" + imgUrl + "&size=468";
				%>
						<a title="<%=obj.getTitle()%>" href="<%=obj.getLink()%>">
							<img alt="<%=obj.getTitle() %>" src="<%=imgUrl%>">
						</a> 
				<%
					}
				%>    
				</div>
			</div>  		
			<%
				}else if(user!=null&&userService.isUserAdmin()){
			%>
			<div class="advWrap adv-468x60 clearafter">
				<div>
					<a href="/admin/advertising/add?place=<%=place %>&position=<%=position %>" title="Add Advertising">Add Advertising Here (468 x 60)</a>
				</div>
			</div>
			<%
				}
						if(colour==(listColour.size()-1)){
							colour=0;	
						}else{
							colour++;	
						}     
						position++;
						}
					}
				}
            }else{
				PlaylistModel plModel = new PlaylistModel();												            		
            	plModel.prepareHomeList(true);
				if(plModel.getListResult().size()>0){
					int colour = 0;			
					int position = 1;
					for(Playlist pl : plModel.getListResult()){						
						TubeSearchModel model = new TubeSearchModel();                         	
                        model.setPage(1);
                        model.prepareHomePageTubeByPlaylist(pl.getId());
                        if(model.getListResult().size()>0){
                        	newApp = false;
                        	Advertising obj = AdvertisingModel.getByPlaceAndPosition(1, position);
			%>
	        <section>
				<div class="block">
					<h3 class="button <%=listColour.get(colour)%>"><a href="/playlist/<%=pl.getId() %>/<%=pl.getAlias()%>"><%=pl.getTitle() %></a><a class="viewall" href="/playlist/<%=pl.getId()%>/<%=pl.getAlias()%>">View All</a></h3>
					<div class="video-listing clearafter">
                	<%	                            	
						for(Tube tub : model.getListResult()){	                            		
							String imgUrl = tub.getImageUrl();
							if(!imgUrl.contains("http://")){
								imgUrl = "/image?key=" + imgUrl + "&size=195";															
							}
					%>
						<div class="video-items" id="<%=tub.getId()%>">	                                														
			                <div class="video-items_inner">
								<a href="/video/<%=tub.getId() %>/<%=tub.getAlias()%>.html" title="<%=tub.getTitle()%>" class="image-resize">
									<img src="<%=imgUrl %>" alt="<%=tub.getTitle()%>" title="<%=tub.getTitle()%>"/>
								</a>               
								<h3 class="video-title"><a href="/video/<%=tub.getId() %>/<%=tub.getAlias() %>.html"><%=tub.getSubTitle() %></a></h3>
								<div class="preview">
									<a href="/video/<%=tub.getId() %>/<%=tub.getAlias() %>.html" class="button <%=listColour.get(colour)%> time"><span class="icon-clock"><%=tub.getTotalTime()%></span></a>
	                            	<a href="http://www.youtube.com/embed/<%=tub.getId() %>?autoplay=1&loop=1&playlist=<%=tub.getId() %>" class="button <%=listColour.get(colour)%> repeat various fancybox.iframe"><span class="icon-monitor">Cinematic</span></a>
	                            </div>
							</div>
		                </div>  
                	<%
						}
					%>                                
                	</div>
				</div>
			</section>
			<%
				if(obj!=null&&obj.getId().length()>0){
			%>
			<div class="advWrap adv-468x60 clearafter" style="border: none;">
				<div>
				<%
					if(obj.getType()==1){
				%>
					<%=obj.getContent() %>	
				<%
					}else{
						String imgUrl = obj.getContent();
						imgUrl = "/image?key=" + imgUrl + "&size=468";	
				%>
						<a title="<%=obj.getTitle()%>" href="<%=obj.getLink()%>">
							<img alt="<%=obj.getTitle() %>" src="<%=imgUrl%>">
						</a> 
				<%
					}
				%>  
				</div>
			</div>  		
			<%
				}else if(user!=null&&userService.isUserAdmin()){
			%>
			<div class="advWrap adv-468x60 clearafter">
				<div>
					<a href="/admin/advertising/add?place=<%=place %>&position=<%=position %>" title="Add Advertising">Add Advertising Here (468 x 60)</a>
				</div>
			</div>
			<%
				}			
               				if(colour==(listColour.size()-1)){
               					colour=0;	
                			}else{
                				colour++;	
                			}     
               				position++;
                       	}
					}
				}
         	}
			%>						
		</div>		
		<jsp:include page="/element/right-content-home.jsp">
			<jsp:param name="isAdmin" value="<%=user!=null&&userService.isUserAdmin()?'1':'0' %>"/>	 
		</jsp:include>
		<%
			if(newApp){
		%>
			<script type="text/javascript">
				window.location.href= "/new-application";
			</script>
		<%
				}
		%>               
		<div class="clearbreak"></div>
	</div>
</div>
<script src="/js/prettyPhoto/js/jquery.prettyPhoto.js" type="text/javascript"></script>		
<script type='text/javascript' src='/js/jquery.aviaSlider.min.js'></script>
<script type='text/javascript' src='/js/custom.min.js'></script>
<script type="text/javascript">		
	$(document).ready(function() {
		$(".various").fancybox({
			maxWidth	: 800,
			maxHeight	: 600,
			fitToView	: false,
			width		: '60%',
			height		: '60%',
			autoSize	: false,
			closeClick	: true,
			closeBtn    : false,
			openEffect	: 'fade',
			closeEffect	: 'fade'
		});	
	});					
</script>	
<jsp:include page="/element/footer.jsp">
	<jsp:param name="analyticCode" value="<%= analyticCode %>"/>
</jsp:include>